package com.hcr.action;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.aspectj.weaver.patterns.ThisOrTargetAnnotationPointcut;
import org.hibernate.Session;
import org.springframework.stereotype.Component;

import com.hcr.commons.DocumentPage;
import com.hcr.commons.IsAdmin;

import com.hcr.model.Document;

import com.hcr.service.DocumentService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Component("documentAction")
public class DocumentAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = -9189777796673668984L;

	private DocumentPage documentPage;
	private DocumentService documentService;
	private String path;
	private String filename;
	private File document;
	private String documentFileName;
	private Integer createuser;
	public Integer getCreateuser() {
		return createuser;
	}
	public void setCreateuser(Integer createuser) {
		this.createuser = createuser;
	}
	public DocumentService getDocumentService() {
		return documentService;
	}
	@Resource(name="documentService")
	public void setDocumentService(DocumentService documentService) {
		this.documentService = documentService;
	}
	public DocumentPage getDocumentPage() {
		return documentPage;
	}
	@Resource(name="documentPage")
	public void setDocumentPage(DocumentPage documentPage) {
		this.documentPage = documentPage;
	}
	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
	}
	public String getFilename() {
		return filename;
	}
	public void setFilename(String filename) {
		this.filename = filename;
	}
	public File getDocument() {
		return document;
	}
	public void setDocument(File document) {
		this.document = document;
	}
	public String getDocumentFileName() {
		return documentFileName;
	}
	public void setDocumentFileName(String documentFileName) {
		this.documentFileName = documentFileName;
	}
	public String deleteDocument(){
		HttpServletRequest request=ServletActionContext.getRequest();
		String id=request.getParameter("id");
		documentService.deleteById(Integer.parseInt(id));
		this.getDocuments();
		return "showdocument";
	}
	public String deletePhoto(){
		HttpServletRequest request=ServletActionContext.getRequest();
		String id=request.getParameter("id");
		documentService.deleteById(Integer.parseInt(id));
		this.getDocuments();
		return "showphoto";
	}
	public void getDocuments(){
		ActionContext context=ActionContext.getContext();	
		List<Document> documents=this.documentPage.firstPage();//不应该是firstPage
		context.put("documents", documents);
	}
	
	public void getPhotos(){
		ActionContext context=ActionContext.getContext();	
		List<Document> documents=this.documentPage.imageFirstPage();//不应该是firstPage
		context.put("documents", documents);
	}
	
	public String saveDocument()throws Exception{
		if (document == null)
			return "error";
		//检查是否存在upload文件夹，若不存在，则新建一个
		String parentServerRealPath = ServletActionContext.getServletContext()
		.getRealPath("/upload/document"+ "\\");
		File parentFile=new File(parentServerRealPath);
		if(!parentFile.exists()){
			parentFile.mkdirs();
		}
		String serverRealPath = ServletActionContext.getServletContext()
				.getRealPath("/upload/document") + "\\" + documentFileName;
		File uploadFile = new File(serverRealPath);
		
		FileUtils.copyFile(document, uploadFile);
		String dateBasepath = "/upload/document/" + documentFileName;
		Document document1=new Document();
		document1.setName(documentFileName);
		document1.setPath(dateBasepath);
		document1.setCreateuser(createuser);
		document1.setType("document");
		this.documentService.add(document1);
		this.getDocuments();
			return "showdocument";
		
		}
	
	
	public String savePhoto()throws Exception{
		if (document == null)
			return "error";
		//检查是否存在upload文件夹，若不存在，则新建一个
		String parentServerRealPath = ServletActionContext.getServletContext()
		.getRealPath("/upload/document"+ "\\");
		File parentFile=new File(parentServerRealPath);
		if(!parentFile.exists()){
			parentFile.mkdirs();
		}
		String serverRealPath = ServletActionContext.getServletContext()
				.getRealPath("/upload/document") + "\\" + documentFileName;
		File uploadFile = new File(serverRealPath);
		
		FileUtils.copyFile(document, uploadFile);
		String dateBasepath = "/upload/document/" + documentFileName;
		Document document1=new Document();
		document1.setName(documentFileName);
		document1.setPath(dateBasepath);
		document1.setCreateuser(createuser);
		document1.setType("photo");
		this.documentService.add(document1);
		this.getDocuments();
		return "showphoto";
	}
	public String showDocument(){
		this.getDocuments();
		if(IsAdmin.isAdmin()){
			return "success";
		}else{
			return "success-base";
		}
		
	}
	public String showPhoto(){
		this.getPhotos();
		if(IsAdmin.isAdmin()){
			return "successphoto";
		}else{
			return "successphoto_base";
		}
		
	}
	public String firstPage(){
		List<Document> documents=this.documentPage.firstPage();
		ActionContext context=ActionContext.getContext();	
		context.put("documents", documents);
		if(IsAdmin.isAdmin()){
			return "success";
		}else{
			return "success-base";
		}
	}
	public String imageFirstPage(){
		List<Document> documents=this.documentPage.imageFirstPage();
		ActionContext context=ActionContext.getContext();	
		context.put("documents", documents);
		if(IsAdmin.isAdmin()){
			return "successphoto";
		}else{
			return "successphoto_base";
		}
	}
	public String nextPage(){
		List<Document> documents=this.documentPage.nextPage();
		ActionContext context=ActionContext.getContext();	
		context.put("documents", documents);
		if(IsAdmin.isAdmin()){
			return "success";
		}else{
			return "success-base";
		}
	}
	public String imageNextPage(){
		List<Document> documents=this.documentPage.imageNextPage();
		ActionContext context=ActionContext.getContext();	
		context.put("documents", documents);
		if(IsAdmin.isAdmin()){
			return "successphoto";
		}else{
			return "successphoto_base";
		}
	}
	public String previousPage(){
		System.out.println(this.documentPage.getCurrentPage());
		List<Document> documents=this.documentPage.previousPage();
		ActionContext context=ActionContext.getContext();
		context.put("documents", documents);
		if(IsAdmin.isAdmin()){
			return "success";
		}else{
			return "success-base";
		}
	}
	public String iamgePreviousPage(){
		List<Document> documents=this.documentPage.imagePreviousPage();
		ActionContext context=ActionContext.getContext();
		context.put("documents", documents);
		if(IsAdmin.isAdmin()){
			return "successphoto";
		}else{
			return "successphoto_base";
		}
	}
	public String addDocument(){
		return "add";
	}
	public String addPhoto(){
		return "addphoto";
	}
	public String getName(String  name,File parentFile){
		boolean flag=true;
		int temp=1;
		String [] names=parentFile.list();
		List<String > list=new ArrayList<String>();
		for(int i=0;i<names.length;i++){
			list.add(names[i]);
		}
		while(flag){
			if(list.contains(name)){
				name=name+temp;
			}else{
				flag=false;
			}
		}
	
		return name;
	}

}
